//
//  ContentCollectionView.m
//  BeautifulLockerRoom
//
//  Created by mac on 16/7/29.
//  Copyright © 2016年 Winny. All rights reserved.
//

#import "ContentCollectionView.h"
#import "ContentCollectionViewCell.h"
#import "ContentReusableView.h"
#import "ShopViewController.h"

@interface ContentCollectionView ()<UICollectionViewDelegate,UICollectionViewDataSource>

@end

@implementation ContentCollectionView

-(instancetype)initWithFrame:(CGRect)frame collectionViewLayout:(UICollectionViewLayout *)layout
{
    if (self= [super initWithFrame:frame collectionViewLayout:layout])
    {

        [self registerClass:[ContentCollectionViewCell class] forCellWithReuseIdentifier:@"cell"];
        [self registerClass:[ContentReusableView class] forSupplementaryViewOfKind:UICollectionElementKindSectionHeader withReuseIdentifier:@"Reusable"];
        self.backgroundColor = [UIColor colorWithRed:243/255.0 green:243/255.0 blue:243/255.0 alpha:1];
        
        self.delegate = self;
        self.dataSource = self;
    }
    return self;
}

- (NSInteger)numberOfSectionsInCollectionView:(UICollectionView *)collectionView {

    return 1;
}


- (NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section {
    return _dataArray.count;
}


- (ContentCollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath
{
    ContentCollectionViewCell * cell = [collectionView dequeueReusableCellWithReuseIdentifier:@"cell" forIndexPath:indexPath];
    DataModel * model = _dataArray[indexPath.item];
    cell.imgView.image = [UIImage imageWithData:[NSData dataWithContentsOfURL:[NSURL URLWithString:model.pic_url]]];
    cell.numberLabel.text = model.sales_volume;
    cell.priceLabel.text = [NSString stringWithFormat:@"%@%@",model.money_symbol,model.selling_price];
    cell.contentLabel.text = model.title;
    
    return cell;
}

-(ContentReusableView *)collectionView:(UICollectionView *)collectionView viewForSupplementaryElementOfKind:(NSString *)kind atIndexPath:(NSIndexPath *)indexPath
{
    ContentReusableView * reusableView = [collectionView dequeueReusableSupplementaryViewOfKind:UICollectionElementKindSectionHeader withReuseIdentifier:@"Reusable" forIndexPath:indexPath];
    if (!reusableView) {
        reusableView = [[ContentReusableView alloc]initWithFrame:CGRectMake(0, 0, WINDOWWIDTH, 720)];
    }
    
    return reusableView;
}
- (void)collectionView:(UICollectionView *)collectionView didSelectItemAtIndexPath:(NSIndexPath *)indexPath;
{
    DataModel * model = _dataArray[indexPath.item];
    ShopViewController * vc = [[ShopViewController alloc]init];
    vc.view.frame = CGRectMake(0, 0, WINDOWWIDTH, WINDOWHEIGHT);
    vc.url = model.mobile_cps_url;
    UIWindow * window = [[UIApplication sharedApplication].delegate window];
    [window.rootViewController presentViewController:vc animated:YES completion:nil];
}
@end
